<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        span {
            color: red;
            font-size: 40px;
        }
    </style>
</head>
<body>
    <p>需求：每当age被修改时，都新增一个历史记录。</p>
    <p>age被修改的次数：<span id="span"></span>次</p>
    <script>
        let num = 0;
        span.innerHTML = num;
        // 被代理的原始数据
        let obj = {age: 18};
        let p1 = new Proxy(obj, {
            set(target, key, val) {
                // 数据被正常的修改
                target[key] = val;
                // 记录被修改的次数
                num ++;
                span.innerHTML = num;
                return true
            }
        })
    </script>
</body>
</html>